package com.hkts.management.message.msg.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.common.vo.UserBasicInfoVo;
import com.hkts.management.message.fegin.IUserAppServiceClient;
import com.hkts.management.message.msg.entity.MsgRecord;
import com.hkts.management.message.msg.mapper.MsgRecordMapper;
import com.hkts.management.message.msg.service.IMsgRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-05-10
 */
@Service
@Transactional
public class MsgRecordServiceImpl extends ServiceImpl<MsgRecordMapper, MsgRecord> implements IMsgRecordService {


    @Autowired
    private MsgRecordMapper msgRecordMapper;
    @Autowired
    private IUserAppServiceClient userAppServiceClient;

    @Override
    public IPage<MsgRecord> selectPage(Page<MsgRecord> page, MsgRecord msgRecord) {
        QueryWrapper<MsgRecord> query = Wrappers.query();
        QueryWrapper<MsgRecord> entityWrapper = getEntityWrapper(query, msgRecord);
        return msgRecordMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<MsgRecord> selectList(MsgRecord msgRecord) {
        QueryWrapper<MsgRecord> query = Wrappers.query();
        QueryWrapper<MsgRecord> entityWrapper = getEntityWrapper(query, msgRecord);
        return msgRecordMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<MsgRecord> getEntityWrapper(QueryWrapper<MsgRecord> entityWrapper, MsgRecord msgRecord) {
        //条件拼接

        return entityWrapper;
    }

    /**
     * 发送下标
     * @param userId
     * @return
     */
    public Integer getSubscript(String userId,Integer sysTem){
        QueryWrapper<MsgRecord> query = Wrappers.query();
        query.lambda().eq(MsgRecord::getToUserId,userId)
                .eq(!MsgRecord.SYSTEM_ALL.equals(sysTem),MsgRecord::getSmsType,sysTem)
            .eq(MsgRecord::getIsRead,0);
        return msgRecordMapper.selectCount(query);

    }

    @Override
    public Result<List<MsgRecord>> getRecordList(String id, Integer size,Integer sysType) {
        UserBasicInfoVo hontUserTo = userAppServiceClient.remoteUserBasicInfo().getResult();
        if(ObjectUtils.isEmpty(hontUserTo)){
            return ResultUtil.error("用户信息有误");
        }
        QueryWrapper<MsgRecord> query = Wrappers.query();
        query.lambda().eq(MsgRecord::getToUserId,hontUserTo.getId())
                .eq(MsgRecord::getSmsType,sysType)
                .lt(ObjectUtils.isNotEmpty(id),MsgRecord::getId,id);
        query.orderByDesc("id");
        String limit="limit ";
        if(size!=null && size>0){
             limit+=size;
        }else {
            limit+=10;
        }
            query.last(limit);
       List<MsgRecord> msgRecords= msgRecordMapper.selectList(query);
        return ResultUtil.data(msgRecords);
    }

    @Override
    public Result<List<MsgRecord>> getRecordsByTime(LocalDateTime startTime, LocalDateTime endTime, Integer sysType) {
        UserBasicInfoVo hontUserTo = userAppServiceClient.remoteUserBasicInfo().getResult();
        if(ObjectUtils.isEmpty(hontUserTo)){
            return ResultUtil.error("用户信息有误");
        }
        QueryWrapper<MsgRecord> query = Wrappers.query();
        query.lambda().ge(MsgRecord::getCreateTime,startTime)
                .lt(MsgRecord::getCreateTime,endTime)
                .eq(MsgRecord::getToUserId,hontUserTo.getId())
                .eq(MsgRecord::getSmsType,sysType);
        query.orderByDesc("id");
        List<MsgRecord> msgRecords= msgRecordMapper.selectList(query);
        return ResultUtil.data(msgRecords);
    }

    @Override
    public Result<Object> changeRecordStatus(String id) {
        MsgRecord msgRecord=msgRecordMapper.selectById(id);
        msgRecord.setIsRead(1);
        msgRecordMapper.updateById(msgRecord);
        return ResultUtil.success();
    }

    @Override
    public int getRecordCount(String messageId) {
        QueryWrapper<MsgRecord> query = Wrappers.query();
        query.lambda().eq(MsgRecord::getMessageId,messageId);
        return msgRecordMapper.selectCount(query);
    }
}
